package com.example.demo.controller.web;

import com.example.demo.entity.Article;
import com.example.demo.service.web.ArticleWebService;
import com.example.demo.service.web.LabelWebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * ArticleController class
 *
 * @author yjq
 * @date 2018/7/30
 */
@RequestMapping("/web/article")
@Controller
public class ArticleWebController {

    @Autowired
    private ArticleWebService articleWebService;
    @Autowired
    private LabelWebService labelWebService;

    /**
     * 跳转至web_articleList界面
     *
     * @param model 数据模型封装
     * @return String
     */
    @RequestMapping("/selectArticle")
    public String toSelectArticle(Model model) {
        model.addAttribute("article", articleWebService.selectAll());
        return "web_articleList";
    }

    /**
     * 跳转至web_selectArticle界面
     *
     * @param articleName 文章名称
     * @param page        页数
     * @param model       数据模型封装
     * @return String
     */
    @RequestMapping(value = "/selectByKey")
    public String selectByKey(String articleName, int page, Model model) {
        List<Article> list = articleWebService.selectByName(articleName);
        int start = (page - 1) * 5;
        if (start + 4 > list.size()) {
            model.addAttribute("article", list.subList(start, list.size()));
        } else {
            model.addAttribute("article", list.subList(start, start + 5));
        }
        model.addAttribute("label", labelWebService.selectLabel());
        model.addAttribute("articleName", articleName);
        model.addAttribute("articleTop7", articleWebService.selectTop7());
        model.addAttribute("articleListenWorld6", articleWebService.articleListenWorld6());
        model.addAttribute("number", (list.size() - 1) / 5 + 1);
        model.addAttribute("page", page);
        return "web_selectArticle";
    }

    /**
     * 查询所有文章
     *
     * @return List<Article>
     * @author yjq
     */
    @RequestMapping(value = "/selectAll", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public List<Article> selectAllArticle() {
        return articleWebService.selectAll();
    }

    /**
     * 查询所有文章
     *
     * @return List<Article>
     * @author yjq
     */
    @RequestMapping("/selectByArticleTitle")
    @ResponseBody
    public List<Article> selectByArticleTitle(String articleTitle) {
        return articleWebService.selectByArticleTitle(articleTitle);
    }

    /**
     * 跳转列表list页
     *
     * @param articleType 文章类型
     * @param page        页数
     * @param model       数据模型封装
     * @return String
     * @author yjq
     */
    @RequestMapping(value = "/list")
    public String list(String articleType, int page, Model model) {
        List<Article> list = articleWebService.selectByType(articleType);
        int start = (page - 1) * 5;
        model.addAttribute("label", labelWebService.selectLabel());
        if (start + 4 > list.size()) {
            model.addAttribute("article", list.subList(start, list.size()));
        } else {
            model.addAttribute("article", list.subList(start, start + 5));
        }
        model.addAttribute("articleType", articleType);
        model.addAttribute("articleTop7", articleWebService.selectTop7());
        model.addAttribute("articleListenWorld6", articleWebService.articleListenWorld6());
        model.addAttribute("number", (list.size() - 1) / 5 + 1);
        model.addAttribute("page", page);
        return "web_articleList";
    }

    /**
     * 跳转详情web_articleDetail页
     *
     * @param id    文章id
     * @param model 数据模型封装
     * @return String
     * @author yjq
     */
    @RequestMapping(value = "/detail")
    public String detail(Integer id, Model model) {
        Article article = articleWebService.selectById(id);
        model.addAttribute("label", labelWebService.selectLabel());
        model.addAttribute("article", article);
        model.addAttribute("articleTop7", articleWebService.selectTop7());
        model.addAttribute("articleListenWorld6", articleWebService.articleListenWorld6());
        model.addAttribute("inner", article.getArticleInner());
        return "web_articleDetail";
    }

}
